<?php
/**
 * 商品云组件分类Control
 * @file 	CategoryControl.class.php
 * @author 	kelecode@gmail.com
 * @date 	2013-06-15
 * modify
 */
class CategoryControl extends Control{

	public function __construct(){
		parent::__construct();
		
		$css = APP_ROOT . '/template/' . C('theme') . '/common/css.php';
		$script =  APP_ROOT . '/template/' . C('theme') . '/common/js.php';
		$mainSubMenu = APP_ROOT . '/component/com_items/template/' . C('theme') . '/common/main_sub_menu.php';
		$this->assign('css',$css);
		$this->assign('script',$script);
		$this->assign('mainSubMenu',$mainSubMenu);
	}

	public function getPath($method){
		$path = '';
		$obj =  explode('::',strtolower($method));
		$class = str_replace('control','',$obj[0]);
		$method = $obj[1];
		$path =  str_replace('\\','/',substr(dirname(__FILE__),0,-7)) . 'template/' . C('theme')  . '/' . $class . '/' . $method . '.php';

		return $path;
	}

	public function index(){
		$this->lists();
	}

	/**
	 * 分类列表
	 */
	public function lists($from=null){
		$items = M('Category')->getCategories();
		$categories = array();
		$lists = array();
		foreach($items['lists'] as $key => $val){
			$pid = $val['parent_id'];
			$id = $val['id'];
			if($pid == 0){
				$categories[$id] = $val;
				$categories[$id]['sub'] = array();
			}else{
				$categories[$pid]['sub'][] = $val;
			}
		}		
		foreach($categories as $category){
			$sublist = $category['sub'];
			unset($category['sub']);
			$lists[] = $category;
			if( is_array($sublist) && !empty($sublist) ){
				foreach($sublist as $sub){
					$lists[] = $sub;
				}
			}
		}
		
		if($from == 'backend'){
			return $lists;
		}

		$tinyPage = new TinyPage( $items['count'],C('page.page_size') );
        $page = $tinyPage->show();
		$main = $this->getPath(__METHOD__);

		$this->assign('main',$main);
		$this->assign('lists',$lists);
		$this->assign('page',$page);
		$this->display('layout','@.common');
	}

	/**
	 * 新加分类
	 */
	public function add($type='add'){
		if($_POST['dosubmit'] == 1){
			$data['id'] = (int)$_POST['id'];
			$data['parent_id'] = (int)$_POST['parent_id'];
			$data['name'] = trim($_POST['name']);
			$data['sort'] = (int)$_POST['sort'];
			$data['status'] = (int)$_POST['status'];
			$data['description'] = trim($_POST['description']);

			if($data['name'] == ''){
				$url = C('backend').'index.php?com=items&c=category&m=add';
				$msg = '分类名称不能为空...';
				redirect($url,2,$msg);
			}

			if(empty($data['id'])){
				$result = M('Category')->addCategory($data);
			}else{
				$result = M('Category')->editCategory($data);
			}

			$url = C('backend').'index.php?com=items&c=category&m=lists';
			$msg = '操作'.($result ? '成功' : '失败').'...';
			$msg = iconv('utf-8','gbk',$msg);
			redirect($url,2,$msg);
		}else{
			$id = (int)$_GET['id'];
			if($id){
				$category = M('Category')->getCategoryById($id);
			}
			$lists = M('Category')->getCategoryByPid(0);
			$main = $this->getPath(__METHOD__);

			$this->assign('main',$main);
			$this->assign('type',$type);
			$this->assign('category',$category);
			$this->assign('lists',$lists);
			$this->assign('id',$id);
			$this->assign('token',token());
			$this->display('layout','@.common');
		}
	}

	/**
	 * 删除分类
	 * @return json
	 */
	public function del(){
		$id = (int)$_POST['id'];
		$result = M('Category')->delCategory($id);
		$info = '删除'.($result ?  '成功' : '失败');
		$status = (int)$result;
		ajaxReturn('',$info,$status);
	}

	/**
	 * 编辑分类
	 */
	public function edit(){
		$this->add('edit');
	}

	/**
	 * 更新状态
	 */
	public function updateStatus(){
		$ids = getgpc('ids');
		empty($ids) && ajaxReturn('','请先选择要操作的商品分类',0);
		$ids = explode(',',$ids);
		$status = (int)getgpc('status');
		$result = M('Category')->updateStatus($ids, $status);
		$info = '操作'.($result ?  '成功' : '失败');
		$status = (int)$result;
		ajaxReturn('',$info,$status);
	}

	/**
	 * 更新商品分类排序
	 */
	public function updateSort(){
		$id = getgpc('id');
		empty($id) && ajaxReturn('','请先选择要操作的商品分类',0);
		$num = (int)getgpc('num');
		$num = $num <= 0 ? 0 : $num;
		$result = M('Category')->updateSort($id, $num);
		$info = '操作'.($result ?  '成功' : '失败');
		$status = (int)$result;
		ajaxReturn('',$info,$status);
	}


}